package com.fanshuai.hash;

import java.util.Iterator;

public class ArrayHashTable implements HashTable {
    public static class Entry {
        public String key;
        public int value;

        public Entry(String key, int value) {
            this.key = key;
            this.value = value;
        }
    }

    private Entry[] arr;
    private int size;
    private int defaultSize = 16;
    private double factor = 0.5;

    private int LIMIT = 4 * 1024 * 1024;

    public ArrayHashTable(int capacity) {
        arr = new Entry[capacity];
        size = 0;
    }

    public ArrayHashTable() {
        arr = new Entry[defaultSize];
        size = 0;
    }

    private int getHash(String key) {
        return key.hashCode() % arr.length;
    }

    private int calcSize() {
        if (2 * arr.length < LIMIT) {
            return 2 * arr.length;
        } else {
            return arr.length + LIMIT;
        }
    }

    private void resize() {
        int newSize = calcSize();
        Entry[] newArr = new Entry[newSize];

        for (int i = 0; i < arr.length; i++) {
            if (arr[i] == null) {
                continue;
            }

            Entry entry = arr[i];
            int newIndex = entry.key.hashCode() % newSize;
            while (newArr[newIndex] != null) {
                newIndex = (newIndex + 1) % newSize;
            }
            newArr[newIndex] = new Entry(entry.key, entry.value);
        }

        arr = newArr;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void insert(String key, int value) {
        if ((size + 1.0) / arr.length >= factor) {
            resize();
        }

        int index = getHash(key);
        while (arr[index] != null) {
            index = (index + 1) % arr.length;
        }

        arr[index] = new Entry(key, value);
        size++;
    }

    @Override
    public Integer find(String key) {
        int index = getHash(key);
        while (arr[index] != null) {
            if (arr[index].key.equals(key)) {
                return arr[index].value;
            }

            index = (index + 1) % arr.length;
        }

        return null;
    }

    @Override
    public void delete(String key) {
        int index = getHash(key);
        while (arr[index] != null) {
            if (arr[index].key.equals(key)) {
                arr[index] = null;
                size--;
                break;
            }

            index = (index + 1) % arr.length;
        }
    }

    @Override
    public boolean contains(String key) {
        return find(key) != null;
    }

    @Override
    public Iterator<Integer> iterator() {
        return new Iterator<Integer>() {
            int index = 0;
            int visitSize = 0;

            @Override
            public boolean hasNext() {
                return visitSize < size;
            }

            @Override
            public Integer next() {
                while (arr[index] == null) {
                    index++;
                }
                int value = arr[index].value;

                index++;
                visitSize++;
                return value;
            }
        };
    }
}
